package structural.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

/**
 * @author wangjinqiang
 * @date 2019-08-05
 */
public class Client {
    public static void main(String[] args) {
        GamePlayer gamePlayer = new GamePlayer("李四");

        IGamePlayer player = (IGamePlayer) Proxy.newProxyInstance(Client.class.getClassLoader(), new Class[]{IGamePlayer.class}, new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                if (method.getName().equalsIgnoreCase("killBoss")) {
                    System.out.println("@代理类开始打Boss@");
                }
                return method.invoke(gamePlayer, args);
            }
        });
        player.killBoss();
        player.upgrade();

    }

    private static void staticProxy() {
        IGamePlayer gamePlayer = new GamePlayer("张三");
        IGamePlayer proxy = new GamePlayerProxy(gamePlayer);
        System.out.println("开始时间是: " + System.currentTimeMillis());
        proxy.login("zhangSan", "password");
        proxy.killBoss();
        proxy.upgrade();
        System.out.println("结束时间是：" + System.currentTimeMillis());
    }
}
